<template>
  <div>
    <el-form
      :model="ChangePwdForm"
      :rules="rules"
      ref="ChangePwdForm"
      label-width="100px"
      class="demo-ChangePwdForm"
    >
      <el-form-item label="原密码" prop="OldPassword">
        <el-input
          placeholder="请输入原密码"
          v-model="ChangePwdForm.OldPassword"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="NewPassword">
        <el-input
          v-model="ChangePwdForm.NewPassword"
          placeholder="请输入新密码"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="ConfirmNewPassword">
        <el-input
          v-model="ChangePwdForm.ConfirmNewPassword"
          placeholder="请确认新密码"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ChangePwdForm')"
          >立即修改</el-button
        >
        <el-button @click="resetForm('ChangePwdForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>



<script>
import User from "@/api/User.js";
const user = new User();
export default {
  data() {
    //   自定义验证
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ChangePwdForm.NewPassword !== "") {
          this.$refs.ChangePwdForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validateConfirmPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ChangePwdForm.NewPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      ChangePwdForm: {
        OldPassword: "",
        NewPassword: "",
        ConfirmNewPassword: "",
      },

      rules: {
        OldPassword: [
          {
            required: true,
            message: "请输入原密码",
            trigger: "change",
          },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        NewPassword: [
          {
            required: true,
            message: "请输入新密码",
            trigger: "change",
          },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
          {
            validator: validatePassword,
            trigger: "blur",
          },
        ],
        ConfirmNewPassword: [
          {
            required: true,
            message: "请确认新密码",
            trigger: "change",
          },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
          {
            validator: validateConfirmPassword,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // console.log(this.ChangePwdForm);
          const response = await user.ChangePassword(
            this.ChangePwdForm,
            this.$store.state.token
          );
          if (response.data.code === 200) {
            //   操作成功
            this.$message.success("操作成功");
            this.resetForm(formName);
          } else {
            this.$message.error(response.data.message);
            this.resetForm(formName);
          }
        } else {
          this.$message.error("非法的输入！");
          this.resetForm(formName);
          return false;
        }
      });
    },
    async resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  components: {},
  async mounted() {},
};
</script> 